package com.anjian.bamin.anjiandianzimap.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aiton.administrator.shane_library.shane.ZjbBaseActivity;
import com.aiton.administrator.shane_library.shane.utils.ACache;
import com.aiton.administrator.shane_library.shane.widget.SideLetterBar;
import com.anjian.bamin.anjiandianzimap.App;
import com.anjian.bamin.anjiandianzimap.R;
import com.anjian.bamin.anjiandianzimap.adapter.MyCompanyAdapter;
import com.anjian.bamin.anjiandianzimap.contant.Constant;
import com.anjian.bamin.anjiandianzimap.model.CompanyInfo;
import com.anjian.bamin.anjiandianzimap.model.CompanyList;
import com.anjian.bamin.anjiandianzimap.utils.MyComparator;
import com.blankj.utilcode.utils.SPUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CompanyListActivity extends ZjbBaseActivity implements View.OnClickListener {

    private ListView mCompanyListView;
    private List<CompanyInfo> mCompanyInfoList = new ArrayList<>();
    private TextView mOverlay;
    private SideLetterBar mLetterBar;
    private MyCompanyAdapter mMyCompanyAdapter;
    private ImageView mImageView_menu;
    private SPUtils mSpUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_list);
        init();
    }

    @Override
    protected void initSP() {
        mSpUtils = new SPUtils(App.getContext(), Constant.SP.SP);

    }

    @Override
    protected void initIntent() {
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void findID() {
        mCompanyListView = (ListView) findViewById(R.id.companyListView);
        Log.e("CompanyListActivity", "findID: --->>" + mCompanyInfoList.size());


        mOverlay = (TextView) findViewById(R.id.tv_letter_overlay);
        mLetterBar = (SideLetterBar) findViewById(R.id.side_letter_bar);
        mImageView_menu = (ImageView) findViewById(R.id.image_menu);
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imgBack).setOnClickListener(this);
        mImageView_menu.setOnClickListener(this);
        mLetterBar.setOverlay(mOverlay);
        mLetterBar.setOnLetterChangedListener(new SideLetterBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                int letterPosition = mMyCompanyAdapter.getLetterPosition(letter);
                mCompanyListView.setSelection(letterPosition);
            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        mCompanyInfoList.clear();
        ACache aCacheCompanyList = ACache.get(CompanyListActivity.this, Constant.ACACHE.COMPANY_LIST_ACACHE);
        CompanyList companyList = (CompanyList) aCacheCompanyList.getAsObject(Constant.ACACHE.COMPANY_LIST);
        List<CompanyInfo> companyInfoList = companyList.getCompanyInfoList();
        mCompanyInfoList.addAll(companyInfoList);
        Collections.sort(mCompanyInfoList,new MyComparator());
        mMyCompanyAdapter = new MyCompanyAdapter(this, mCompanyInfoList);
        mCompanyListView.setAdapter(mMyCompanyAdapter);
        mMyCompanyAdapter.notifyDataSetChanged();

        //重新设置位置
        int mPosition = mSpUtils.getInt(Constant.SP.LIST_POSITION);
        Log.e("CompanyListActivity", "onStart: --->>" +mPosition );
        if(mPosition != -1){
            mCompanyListView.setSelection(mPosition);
        }else{
            mCompanyListView.setSelection(0);

        }

        mMyCompanyAdapter.setOnCompanyClickListener(new MyCompanyAdapter.OnCompanyClickListener() {
            @Override
            public void onCompanyClick(CompanyInfo companyInfo) {
                Intent intent = CompanyDetialActivity.newIntent(CompanyListActivity.this, companyInfo,true);
                startActivityTo(intent);
                //保存当前的位置
                int firstVisiblePosition = mCompanyListView.getFirstVisiblePosition();
                mSpUtils.putInt(Constant.SP.LIST_POSITION,firstVisiblePosition);
                Log.e("CompanyListActivity", "onCompanyClick: --->>" + firstVisiblePosition);



            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgBack:
                finishTo();
                break;
            case R.id.image_menu:
                showPopupMenu(mImageView_menu);
                break;
        }
    }
    private void showPopupMenu(View view) {
        // View当前PopupMenu显示的相对View的位置
        PopupMenu popupMenu = new PopupMenu(this, view);
        // menu布局
        popupMenu.getMenuInflater().inflate(R.menu.menu_companylist, popupMenu.getMenu());
        // menu的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_add:
                        startActivityTo(new Intent(CompanyListActivity.this, AddCompanyActivity.class));
                        break;
                    case R.id.menu_search:
                        startActivityTo(new Intent(CompanyListActivity.this, SearchActivity.class));
                        break;
                }
                return false;
            }
        });
        popupMenu.show();
    }

}
